package com.stock.stockana.ui.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.stock.publiclib.utils.StringUtil;
import com.stock.stockana.R;
import com.stock.stockana.StockCenter;
import com.stock.stockana.beans.ResponseBean;
import com.stock.stockana.constants.NetWorkConstants;
import com.stock.stockana.events.network.UpdateUserEvent;
import com.stock.stockana.requests.UpdateUser;
import com.stock.stockana.ui.activities.SettingActivity;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;

/**
 * Created by ljgsonx on 2016/3/4.
 */
public class MotifyUserPwdFragment extends Fragment {

    private static final String TAG = "MotifyUserPwdFragment->";

    TextView mUserPwdEt;
    TextView mUserPwdAgainEt;
    Button mConfirmBtn;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.motify_userpwd_layout, container, false);
        mUserPwdEt = (EditText) view.findViewById(R.id.userPwdEt);
        mUserPwdAgainEt = (EditText) view.findViewById(R.id.userPwdAgainEt);
        mConfirmBtn = (Button) view.findViewById(R.id.confirmBtn);
        mConfirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userPwd = mUserPwdEt.getText().toString();
                String userPwdAgain = mUserPwdAgainEt.getText().toString();
                if (StringUtil.hasBlank(userPwd, userPwdAgain)) {
                    Toast.makeText(getActivity(), "请输入完整!", Toast.LENGTH_LONG).show();
                }else if(!userPwd.equals(userPwdAgain)){
                    Toast.makeText(getActivity(), "两次输入不一致!", Toast.LENGTH_LONG).show();
                }else {
                    UpdateUser updateUser = UpdateUser.build();
                    updateUser.setPasswd(userPwd);
                    StockCenter.doReq(updateUser);
                    ((SettingActivity) getActivity()).showProgress("正在修改...");
                }
            }
        });
        return view;
    }

    @Subscribe
    public void onEventMainThread(UpdateUserEvent event) {
        ((SettingActivity) getActivity()).stopProgress();
        ResponseBean rb = event.getmResponseBean();
        if (rb.getRespCode() == NetWorkConstants.RESPONSE_CODE_OK) {
            Toast.makeText(getActivity(), "密码修改成功,请牢记新密码!", Toast.LENGTH_LONG).show();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    getFragmentManager().popBackStack();
                }
            }, 800);
        }else{
            Log.d(TAG, rb.getErrorMsg());
            Toast.makeText(getActivity(), "可能由于网络原因修改失败,请重试!", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }


}
